package com.study.spaoplog;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author study
 * @version 1.0
 * @date 2021/6/15 11:12
 */
public class CreateFile {

    private final File file = new File("d:/jvm/12.txt");

    @Test
    public void createFile() throws IOException {
        final List<String> collect = IntStream.rangeClosed(0, 155).boxed().map(item -> "a").collect(Collectors.toList());
        FileUtils.writeLines(file, StandardCharsets.UTF_8.name(), collect, true);

    }

    @Test
    public void readFile() throws IOException {
        Path filePath = Paths.get("d:/jvm/12.txt");
        //try-with-resources语法,不用手动的编码关闭流
        /*try (Stream<String> lines = Files.lines(filePath)) {
            lines.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();//只是测试用例，生产环境下不要这样做异常处理
        }*/
        final File file = filePath.toFile();
        final List<String> list = Files.readAllLines(filePath);
        System.out.println(list.getClass());
        list.forEach(s -> {
            System.out.println(s);
        });


    }
}
